class Solution:
    def maximalSquare(self, matrix):
        """
        :type matrix: List[List[str]]
        :rtype: int
        """
        if len(matrix) == 0 or len(matrix[0]) == 0:
            return 0
        res = 0
        row = len(matrix)
        col = len(matrix[0])
        dp = [[0 for c in range(col+1)] for r in range(row+1)]
        for i in range(1, row+1):
            for j in range(1, col+1):
                if matrix[i-1][j-1] == '1':
                    dp[i][j] = min(min(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1])+1
                    res = max(res, dp[i][j])
        return res*res
